package playing;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class PlayerPlantReading {

    private static final String DB_URL = "jdbc:mysql://localhost:3306/ZUT";
    private static final String USER = "root";
    private static final String PASS = "123456";

    //读取
    public List<Integer> getPlantIdsByUserId(int userId) {
        List<Integer> plantIds = new ArrayList<>();
        String sql = "SELECT plant_id FROM user_plants WHERE user_id = ?";

        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, userId);
            try (ResultSet rs = pstmt.executeQuery()) {
                while (rs.next()) {
                    int plantId = rs.getInt("plant_id");
                    plantIds.add(plantId);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return plantIds;
    }
    //插入
    public void insertPlantForUser(int userId, int plantId) {
        String sql = "INSERT INTO user_plants (user_id, plant_id) VALUES (?, ?)";

        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, userId);
            pstmt.setInt(2, plantId);

            // 执行插入操作
            int rowsAffected = pstmt.executeUpdate();

            // 通常情况下，如果 rowsAffected == 1，则表示插入成功
            if (rowsAffected != 1) {
                System.err.println("Failed to insert plant for user. No rows affected.");
            }

        } catch (SQLException e) {
            e.printStackTrace();
            // 你可以在这里添加更详细的错误处理逻辑
        }
    }
    //删除
    public boolean deletePlantForUser(int userId, int plantId) {
        String sql = "DELETE FROM user_plants WHERE user_id = ? AND plant_id = ?";

        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, userId);
            pstmt.setInt(2, plantId);

            // 执行删除操作
            int rowsAffected = pstmt.executeUpdate();

            // 通常情况下，如果 rowsAffected == 1，则表示删除成功
            if (rowsAffected != 1) {
                return false;
            } else {
                return true;
            }

        } catch (SQLException e) {
            e.printStackTrace();
           return false;
        }
    }
    //更新
    public void updatePlantForUser(int oldUserId, int plantId, int newUserId) {
        String sql = "UPDATE user_plants SET user_id = ? WHERE plant_id = ? AND user_id = ?";

        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, newUserId); // 设置新的用户ID
            pstmt.setInt(2, plantId);   // 设置植物ID
            pstmt.setInt(3, oldUserId); // 设置原始的用户ID用于过滤

            // 执行更新操作
            int rowsAffected = pstmt.executeUpdate();

            // 通常情况下，如果 rowsAffected == 1，则表示更新成功
            if (rowsAffected != 1) {
                System.err.println("错误");
            } else {
                System.out.println("成功");
            }

        } catch (SQLException e) {
            e.printStackTrace();
            // 你可以在这里添加更详细的错误处理逻辑
        }
    }

     //用于测试

//    public static void main(String[] args) {
//        PlayerPlantReading reader = new PlayerPlantReading();
//        List<Integer> plantIdList = reader.getPlantIdsByUserId(6); // 假设用户ID为1
//        if(plantIdList.isEmpty())
//        {
//            System.out.println("查无此人");
//        }
//        else
//        {
//            for (int plantId : plantIdList) {
//            System.out.println("Plant ID: " + plantId);
//        }
//        }
//    }
}